function restrict(e, facetURI) {
	
    var inputElement = getTarget(e);
    var listTable = document.getElementById(facetURI + "-list");
    var text = inputElement.value;
    var regexp = new RegExp(text, "ig");
    
    var rows = listTable.getElementsByTagName("tr");
    for (var i = 0; i < rows.length; i++) {
    	var row = rows[i];
    	
    	if (text.length > 0) {
	    	var cell = row.cells.length > 1 ? row.cells[1] : row.cells[0];
	    	var restrictionValue = cell.firstChild.nodeValue;
	    	
    		if (restrictionValue.match(regexp)) {
    			if (row.style.display != "block") {
    				row.style.display = "block";
    			}
    		} else {
    			if (row.style.display != "none") {
    				row.style.display = "none";
    			}
    		}
    	} else {
    		row.style.display = "block";
    	}
    }
    listTable.style.display = "none";
    listTable.style.display = "block";
}

function getListByFirsChar(buckets,start){
	var list = [];
	for(var i = 0; i<buckets.length;i++){
		if(buckets[i].startsWith(start.toUpperCase()) || buckets[i].startsWith(start)){
			list.push(buckets[i]);
		}
	}
	return list;
}


String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

function isNumber (o) {
	  return ! isNaN (o-0) && o != null;
	}


function restrictCustom(e, buckets,hrefs,counts, facetURI, serverAddress) {
		var inputElement = getTarget(e);
		var text = inputElement.value;
		if(isNumber(text)){
			if(text.length>3){
				buildFacet(buckets,hrefs,counts,facetURI, text);
			}
		}
		else if(text.length==0){
	    	buildFacet(buckets,hrefs,counts,facetURI, "a");
	    }
		else if(text.length==1){
	    	
	    	buildFacet(buckets,hrefs,counts,facetURI, text);   
		}
	    else {
	    	restrict(e, facetURI);
	    }
	   /*
	    var rows = listTable.getElementsByTagName("tr");
	    for (var i = 0; i < rows.length; i++) {
	    	var row = rows[i];
	    	
	    	if (text.length > 3) {
		    	var cell = row.cells.length > 1 ? row.cells[1] : row.cells[0];
		    	var restrictionValue = cell.firstChild.nodeValue;
		    	
	    		if (restrictionValue.match(regexp)) {
	    			if (row.style.display != "block") {
	    				row.style.display = "block";
	    			}
	    		} else {
	    			if (row.style.display != "none") {
	    				row.style.display = "none";
	    			}
	    		}
	    	} else {
	    		row.style.display = "block";
	    	}
	    }
	    listTable.style.display = "none";
	    listTable.style.display = "block";
	
	    */
}

function buildFacet (buckets,hrefs,counts,facetURI,text) {
	
			var listTable = document.getElementById(facetURI + "-list");
		    var tableTmp = listTable;  
		    var regexp = new RegExp(text, "ig");
			var items = getListByFirsChar(buckets,text);
	    	listTable.innerHTML = "";
	    	
	    	for (var i = 0; i < items.length; i++) {
	    		var item = items[i];
	    		
	    		var index = buckets.indexOf(item);
	    		var count = counts[index];
	  
	    		var href = hrefs[index];
	    		//console.debug(href);
	    		var row=document.createElement("TR");
	    		var td = document.createElement("TD");
	    		var quotes='\'';
	    		var tmp = 'browseTo('+quotes+href+quotes+')';
	    		td.setAttribute('onclick',tmp); 
	    		td.setAttribute('class','restriction-link');
	    		td.setAttribute('name','restriction');
	    		var tdNumber = document.createElement("TD");
	    		tdNumber.setAttribute('class','restriction-preview-count');
	    		
	    		var textnode1=document.createTextNode(item);
	    		var number=document.createTextNode(count);
	    		
	    		td.appendChild(textnode1);
	    		tdNumber.appendChild(number);
	    		//console.debug(facetURI);
	    		if(facetURI.indexOf("broadening")==-1){
	    			row.appendChild(tdNumber);
	    		}
	    		row.appendChild(td);
	    		listTable.appendChild(row);	
	    	}
}
function selectBucketTheme(select, name) {
	var selectedIndex = select.selectedIndex;
	
	var bucketThemes = document.getElementsByName(name);
	for (var i = 0; i < bucketThemes.length; i++) {
		var bucketTheme = bucketThemes[i];
		if (i == selectedIndex) {
			bucketTheme.style.visibility = "visible";
		} else {
			bucketTheme.style.visibility = "hidden";
		}
	}
}